<?php

namespace app\applet\controller;

use app\common\controller\Applet;
use app\common\model\broadcast\DeviceRod;
use app\common\model\haikang\HkConsumerMsg;
use app\common\model\haikang\HkConsumerMsgFd;
use app\common\model\haikang\HkConsumerMsgWeek;
use app\common\model\haikang\HkEventType;
use app\common\model\work_area\HkDeviceSerial;
use app\common\model\work_area\WorkAreaStand;
use app\common\service\device\RadarService;
use app\common\service\haikang\HaikangService;
use app\common\service\tencent\VoiceService;
use hg\apidoc\annotation as Apidoc;
use think\facade\Db;
use think\facade\Queue;
use think\facade\Session;

/**
 * @Apidoc\Title("测试相关")
 */
class Test extends Applet
{

    //中间件不验证的方法
    protected $middleware = [
        'app\\middleware\\Applet' => [
            'except' => ['test', 'index', 'yjCallback', 'dataPush', 'broadcastPush']
        ]
    ];


    /**
     * @Apidoc\Title("广播接口")
     * @Apidoc\Url("http://ai.admin.lgsid.com/applet/Test/broadcastPush")
     * @Apidoc\Param("mac_address", type="string",require=true, desc="设备mac地址")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="对象信息",
     *    @Apidoc\Returned("url", type="string", desc="音频播放地址"),
     * )
     */
    public function broadcastPush()
    {
        return $this->ajax(1, '成功', ['url' => '']);
    }

    /**
     * @Apidoc\Title("测试方法")
     * @Apidoc\Url("/applet/Test/test")
     * @Apidoc\Param("test", type="int", desc="默认传0")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function test()
    {
        dd(1);
        $hk_device_serial = HkDeviceSerial::alias('a')
            ->join("work_area_stand b ", " b.id = a.work_area_stand_id", 'left')
            ->join("work_area c ", " c.id = b.work_area_id", 'left')
            ->where(['device_serial' => '486647C4082A00047', 'channel_no' => 0])
            ->field(['a.id', 'a.device_rod_id', 'a.work_area_stand_id', 'a.name', 'b.name bname', 'c.name st_line'])
            ->find();
        // 1.当前任务将由哪个类来负责处理。
        $jobHandlerClassName = 'app\queue\haikang\ConsumerMessagesQueue';
        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
        $jobQueueName = "ConsumerMessagesQueue"; //php think queue:listen --queue ConsumerMessagesQueue
        Queue::later(2, $jobHandlerClassName, $hk_device_serial, $jobQueueName);
    }

    /**
     * @Apidoc\Title("ai雷达传送抓拍文件回调")
     * @Apidoc\Url("/applet/Test/dataPush")
     * @Apidoc\Param("path", type="string", desc="文件实际路径")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function dataPush()
    {
        try {
            $param = request()->param();
            RadarService::dataPush($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        exit;
    }

    /**
     * @Apidoc\Title("昆云预警回调")
     * @Apidoc\Url("/applet/Test/yjCallback")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function yjCallback()
    {
        $param = request()->param();
        if (isset($param['alarmPicData']) && isset($param['cameraName']) && isset($param['algorithmName'])) {
            $hk_consumer_msg = HkConsumerMsg::where(['yj_data_id' => $param['dataID']])->value('id');
            if (empty($hk_consumer_msg)) {
                // 1.当前任务将由哪个类来负责处理。
                $jobHandlerClassName = 'app\queue\yunjin\MsgCallbackQueue';
                // 2.当前任务归属的队列名称，如果为新队列，会自动创建
                $jobQueueName = "MsgCallbackQueue"; //php think queue:listen --queue MsgCallbackQueue
                Queue::push($jobHandlerClassName, $param, $jobQueueName);
            }
        }
    }

    /**
     * @Apidoc\Title("语音合成")
     * @Apidoc\Url("/applet/Test/index")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function index()
    {
        $param = request()->param();
        if (empty($param['Text'])) {
            echo '文字空';
            exit;
        }
        $params = array(
            "Text" => $param['Text'],
            "SessionId" => "work_area" . random_string(3),
            "Volume" => 10,
            "Speed" => 0
        );
        $file = VoiceService::saveVoice($params);
        return $this->ajax(1, '成功', get_file_url($file));
    }

}